import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_module/base/base.dart';
import 'package:flutter_module/page_state.dart';
import 'package:flutter_module/user/login.dart';
import 'package:get/get.dart';

//
class RoutePageState extends Cubit<String> {
  RoutePageState() : super("") {
    routes.addLast(rootRoute);
  }

  // 根路由
  final String rootRoute = "/";

  // 数组
  Queue<String> routes = ListQueue<String>();

  void toLogin() {
    routes.addLast("/login");
    emit(routes.last);
  }

  void back() {
    if (routes.isNotEmpty) {
      String popRoute = routes.removeLast();
      debugPrint(popRoute);
      emit(routes.last);
    } else {
      emit(rootRoute);
    }
  }
}

/// 首页下标状态
class BlocHomeIndexState extends Cubit<int> {
  BlocHomeIndexState() : super(0);

  void upIndex(int index){
    emit(index);
  }
}

class RouteStateApp extends BaseWidget {
  RouteStateApp({super.key});

  BlocHomeIndexState blocHomeIndexState = BlocHomeIndexState();

  @override
  Widget setContent(BuildContext context) {
    return BlocProvider(
      create: (BuildContext context) => RoutePageState(),
      child: BlocBuilder<RoutePageState, String>(
        builder: (context, String route) {
          switch (route) {
            case "/":
              return PageStateView(blocHomeIndexState);
            case "/login":
              return Login();
            default:
              // error
              return PageStateView(blocHomeIndexState);
          }
        },
      ),
    );
  }
}
